package com.example.zyfypt_206shj.fragement;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.RadioGroup;

import com.example.zyfypt_206shj.R;
import com.example.zyfypt_206shj.lib.PagerSlidingTabStrip;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Raindy on 2017/4/29.
 */
//implements ViewPager.OnPageChangeListener

public class Fragment2 extends Fragment {
    private View view;
    private Context context;
//    private RadioGroup rgChannel=null;
//    private ViewPager viewPager;
//    private HorizontalScrollView hvChannel=null;
//    private List<Fragment> fragment2List=new ArrayList<Fragment>();
//    private FragmentPagerAdapter fragmentPagerAdapter;
    private Fragment_project fragment_project;
    private Fragment_tcase fragment_tcase;
    private PagerSlidingTabStrip tabs;
    private DisplayMetrics dm;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        context=getActivity();
        if(view==null){
            view=inflater.inflate(R.layout.fragment2, null);
            initView(view);
//
//            rgChannel=(RadioGroup)view.findViewById(R.id.rgChannel);
//            viewPager=(ViewPager)view.findViewById(R.id.vpNewsList);
//            hvChannel=(HorizontalScrollView)view.findViewById(R.id.hvChannel);
//            rgChannel.setOnCheckedChangeListener(
//                    new RadioGroup.OnCheckedChangeListener() {
//                        @Override
//                        public void onCheckedChanged(RadioGroup group, int checkedId) {
//                            viewPager.setCurrentItem(checkedId);
//                        }
//                    });
//            initTab(inflater);
//            initViewPager();
        }
//        ViewGroup parent=(ViewGroup)view.getParent();
//        if(parent!=null){
//            parent.removeView(view);
//        }
        return view;
    }
    private void initView(View view) {

        dm = getResources().getDisplayMetrics();
        ViewPager pager = (ViewPager) view.findViewById(R.id.pager);
        tabs = (PagerSlidingTabStrip) view.findViewById(R.id.tabs);
        pager.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
        tabs.setViewPager(pager);
        setTabsValue();

    }
    private void setTabsValue() {
        // 设置Tab是自动填充满屏幕的
        tabs.setShouldExpand(true);
        // 设置Tab的分割线是透明的
        tabs.setDividerColor(Color.TRANSPARENT);
        // tabs.setDividerColor(Color.BLACK);
        // 设置Tab底部线的高度
        tabs.setUnderlineHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 1, dm));
        // 设置Tab Indicator的高度
        tabs.setIndicatorHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 4, dm));// 4
        // 设置Tab标题文字的大小
        tabs.setTextSize((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 16, dm)); // 16
        // 设置Tab Indicator的颜色
        tabs.setIndicatorColor(Color.parseColor("#45c01a"));// #45c01a
        // 设置选中Tab文字的颜色 (这是我自定义的一个方法)
        tabs.setSelectedTextColor(Color.parseColor("#45c01a"));// #45c01a
        // 取消点击Tab时的背景色
        tabs.setTabBackground(0);
    }
    public class MyPagerAdapter extends FragmentStatePagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }

        private final String[] titles = { "案例", "项目" };

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:

                    if (null == fragment_tcase) {
                        fragment_tcase = new Fragment_tcase();
                    }

                    return fragment_tcase;

                case 1:

                    if (null == fragment_project) {
                        fragment_project = new Fragment_project();
                    }

                    return fragment_project;
//                case 2:
//
//                    if (null == subFragment3) {
//                        subFragment3 = new SubFragment3();
//                    }
//                    subFragment1 = new SubFragment1();
//                    return subFragment3;
                default:
                    return null;
            }
        }

    }
//
//    private void initViewPager(){
//        fragment2List.add(new Fragment_tcase());
//        fragment2List.add(new Fragment_project());
//        fragmentPagerAdapter = new FragmentPagerAdapter(super.getActivity().getSupportFragmentManager()) {
//            @Override
//            public Fragment getItem(int position) {
//                return fragment2List.get(position);
//            }
//
//            @Override
//            public int getCount() {
//                return fragment2List.size();
//            }
//        };
//        viewPager.setAdapter(fragmentPagerAdapter);
//        viewPager.setOffscreenPageLimit(2);
//        viewPager.setCurrentItem(0);
//        viewPager.setOnPageChangeListener(this);
//    }
//    private void initTab(LayoutInflater inflater){
//        RadioButton rb1=(RadioButton)inflater.inflate(R.layout.tab_rb, null);
//        RadioButton rb2=(RadioButton)inflater.inflate(R.layout.tab_rb, null);
//        rb1.setId(0);
//        rb1.setText("案例");
//        rb2.setId(1);
//        rb2.setText("项目");
//        RadioGroup.LayoutParams params=new
//                RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT,
//                RadioGroup.LayoutParams.MATCH_PARENT,
//                1.0f);
//        rgChannel.addView(rb1,params);
//        rgChannel.addView(rb2,params);
//        rgChannel.check(0);
//    }
//
//    @Override
//    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//
//    }
//
//    @Override
//    public void onPageSelected(int position) {
//        setTab(position);
//    }
//
//    @Override
//    public void onPageScrollStateChanged(int state) {
//
//    }
//    private void setTab(int idx){
//        RadioButton rb=(RadioButton)rgChannel.getChildAt(idx);
//        rb.setChecked(true);
//        int left=rb.getLeft();
//        int width=rb.getMeasuredWidth();
//        DisplayMetrics metrics=new DisplayMetrics();
//        super.getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
//        int screenWidth=metrics.widthPixels;
//        int len=left+width/2-screenWidth/2;
//        hvChannel.smoothScrollTo(len, 0);
//    }
}
